﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using InspectionMAC.SystemThread;
using InspectionMAC.Function;
using csLTDMC;
using LitJson;
using System.IO;

namespace InspectionMAC.Forms
{
    public partial class Param_Form1 : Form
    {
        
        private string configFilePath = "axis_parameters.json";
        public AxisParameters axisParameters = new AxisParameters();
        public Param_Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 4; i++)
            {
                axisParameters.Axes.Add(new AxisConfig());
            }
        }
        // 加载参数
        private void LoadParameters()
        {
            if (File.Exists(configFilePath))
            {
                try
                {
                    string json = File.ReadAllText(configFilePath);
                    axisParameters = JsonMapper.ToObject<AxisParameters>(json);
                    // 更新界面
                    UpdateUIFromParameters();
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"加载参数失败: {ex.Message}");
                }
            }
        }

        // 保存参数
        private void SaveParameters()
        {
            try
            {
                // 从界面更新参数
                UpdateParametersFromUI();

                string json = JsonMapper.ToJson(axisParameters);
                File.WriteAllText(configFilePath, json);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存参数失败: {ex.Message}");
            }
        }

        // 从界面更新参数对象
        private void UpdateParametersFromUI()
        {
            // 轴0参数
            axisParameters.Axes[0].StartSpeed = startSpeedTb.Text;
            axisParameters.Axes[0].RunSpeed = runSpeedTb.Text;
            axisParameters.Axes[0].UpTime = upTimeTb.Text;
            axisParameters.Axes[0].DownTime = downTimeTb.Text;
            axisParameters.Axes[0].StopSpeed = stopSpeedTb.Text;
            axisParameters.Axes[0].S_Time = S_TimeTb.Text;
            axisParameters.Axes[0].MotorCircle = motorCircleTb.Text;
            axisParameters.Axes[0].IsForward = forwardRb.Checked;
            axisParameters.Axes[0].ChangeSpeed = changeSpeedTb.Text;
            axisParameters.Axes[0].ChangeTime = changeTimeTb.Text;

            // 轴1参数
            axisParameters.Axes[1].StartSpeed = startSpeedTb_02.Text;
            axisParameters.Axes[1].RunSpeed = runSpeedTb_02.Text;
            axisParameters.Axes[1].UpTime = upTimeTb_02.Text;
            axisParameters.Axes[1].DownTime = downTimeTb_02.Text;
            axisParameters.Axes[1].StopSpeed = stopSpeedTb_02.Text;
            axisParameters.Axes[1].S_Time = S_TimeTb_02.Text;
            axisParameters.Axes[1].MotorCircle = motorCircleTb_02.Text;
            axisParameters.Axes[1].IsForward = forwardRb_02.Checked;
            axisParameters.Axes[1].ChangeSpeed = changeSpeedTb_02.Text;
            axisParameters.Axes[1].ChangeTime = changeTimeTb_02.Text;

            // 轴2参数
            axisParameters.Axes[2].StartSpeed = startSpeedTb_03.Text;
            axisParameters.Axes[2].RunSpeed = runSpeedTb_03.Text;
            axisParameters.Axes[2].UpTime = upTimeTb_03.Text;
            axisParameters.Axes[2].DownTime = downTimeTb_03.Text;
            axisParameters.Axes[2].StopSpeed = stopSpeedTb_03.Text;
            axisParameters.Axes[2].S_Time = S_TimeTb_03.Text;
            axisParameters.Axes[2].MotorCircle = motorCircleTb_03.Text;
            axisParameters.Axes[2].IsForward = forwardRb_03.Checked;
            axisParameters.Axes[2].ChangeSpeed = changeSpeedTb_03.Text;
            axisParameters.Axes[2].ChangeTime = changeTimeTb_03.Text;

            // 轴3参数
            axisParameters.Axes[3].StartSpeed = startSpeedTb_04.Text;
            axisParameters.Axes[3].RunSpeed = runSpeedTb_04.Text;
            axisParameters.Axes[3].UpTime = upTimeTb_04.Text;
            axisParameters.Axes[3].DownTime = downTimeTb_04.Text;
            axisParameters.Axes[3].StopSpeed = stopSpeedTb_04.Text;
            axisParameters.Axes[3].S_Time = S_TimeTb_04.Text;
            axisParameters.Axes[3].MotorCircle = motorCircleTb_04.Text;
            axisParameters.Axes[3].IsForward = forwardRb_04.Checked;
            axisParameters.Axes[3].ChangeSpeed = changeSpeedTb_04.Text;
            axisParameters.Axes[3].ChangeTime = changeTimeTb_04.Text;

            axisParameters.Cameras[0].C = CameratextBox01.Text;
            axisParameters.Cameras[1].C = CameratextBox02.Text;
            axisParameters.Cameras[2].C = CameratextBox03.Text;
        }

        // 从参数对象更新界面
        private void UpdateUIFromParameters()
        {
            if (axisParameters.Axes.Count < 4) return;

            // 轴0参数
            startSpeedTb.Text = axisParameters.Axes[0].StartSpeed;
            runSpeedTb.Text = axisParameters.Axes[0].RunSpeed;
            upTimeTb.Text = axisParameters.Axes[0].UpTime;
            downTimeTb.Text = axisParameters.Axes[0].DownTime;
            stopSpeedTb.Text = axisParameters.Axes[0].StopSpeed;
            S_TimeTb.Text = axisParameters.Axes[0].S_Time;
            motorCircleTb.Text = axisParameters.Axes[0].MotorCircle;
            forwardRb.Checked = axisParameters.Axes[0].IsForward;
            backRb.Checked = !axisParameters.Axes[0].IsForward;
            changeSpeedTb.Text = axisParameters.Axes[0].ChangeSpeed;
            changeTimeTb.Text = axisParameters.Axes[0].ChangeTime;

            // 轴1参数
            startSpeedTb_02.Text = axisParameters.Axes[1].StartSpeed;
            runSpeedTb_02.Text = axisParameters.Axes[1].RunSpeed;
            upTimeTb_02.Text = axisParameters.Axes[1].UpTime;
            downTimeTb_02.Text = axisParameters.Axes[1].DownTime;
            stopSpeedTb_02.Text = axisParameters.Axes[1].StopSpeed;
            S_TimeTb_02.Text = axisParameters.Axes[1].S_Time;
            motorCircleTb_02.Text = axisParameters.Axes[1].MotorCircle;
            forwardRb_02.Checked = axisParameters.Axes[1].IsForward;
            backRb_02.Checked = !axisParameters.Axes[1].IsForward;
            changeSpeedTb_02.Text = axisParameters.Axes[1].ChangeSpeed;
            changeTimeTb_02.Text = axisParameters.Axes[1].ChangeTime;

            // 轴2参数
            startSpeedTb_03.Text = axisParameters.Axes[2].StartSpeed;
            runSpeedTb_03.Text = axisParameters.Axes[2].RunSpeed;
            upTimeTb_03.Text = axisParameters.Axes[2].UpTime;
            downTimeTb_03.Text = axisParameters.Axes[2].DownTime;
            stopSpeedTb_03.Text = axisParameters.Axes[2].StopSpeed;
            S_TimeTb_03.Text = axisParameters.Axes[2].S_Time;
            motorCircleTb_03.Text = axisParameters.Axes[2].MotorCircle;
            forwardRb_03.Checked = axisParameters.Axes[2].IsForward;
            backRb_03.Checked = !axisParameters.Axes[2].IsForward;
            changeSpeedTb_03.Text = axisParameters.Axes[2].ChangeSpeed;
            changeTimeTb_03.Text = axisParameters.Axes[2].ChangeTime;

            // 轴3参数
            startSpeedTb_04.Text = axisParameters.Axes[3].StartSpeed;
            runSpeedTb_04.Text = axisParameters.Axes[3].RunSpeed;
            upTimeTb_04.Text = axisParameters.Axes[3].UpTime;
            downTimeTb_04.Text = axisParameters.Axes[3].DownTime;
            stopSpeedTb_04.Text = axisParameters.Axes[3].StopSpeed;
            S_TimeTb_04.Text = axisParameters.Axes[3].S_Time;
            motorCircleTb_04.Text = axisParameters.Axes[3].MotorCircle;
            forwardRb_04.Checked = axisParameters.Axes[3].IsForward;
            backRb_04.Checked = !axisParameters.Axes[3].IsForward;
            changeSpeedTb_04.Text = axisParameters.Axes[3].ChangeSpeed;
            changeTimeTb_04.Text = axisParameters.Axes[3].ChangeTime;

            CameratextBox01.Text = axisParameters.Cameras[0].C;
            CameratextBox02.Text = axisParameters.Cameras[1].C;
            CameratextBox03.Text = axisParameters.Cameras[2].C;
        }

        // 修改窗体关闭事件
        private void Param_Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            SaveParameters(); // 保存参数
            //MacTestForm.param_Form1 = null;
        }

        // 修改窗体加载事件
        private void Param_Form_Load(object sender, EventArgs e)
        {
            timer1.Start();
            LoadParameters(); // 加载参数
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            SaveParameters();
            MessageBox.Show("参数已保存");
        }

        // 添加加载按钮事件（可选）
        private void loadBtn_Click(object sender, EventArgs e)
        {
            LoadParameters();
            MessageBox.Show("参数已加载");
        }

        #region checkBox
        //启动上料机构(预备)
        private void checkBox0_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox0.Checked)
            {
                DIO_Thread.Do[0] = 1;
            }
            else
            {
                DIO_Thread.Do[0] = 0;
            }
        }
        //触发工位一相机及光源
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                DIO_Thread.Do[1] = 1;
            }
            else
            {
                DIO_Thread.Do[1] = 0;
            }
        }
        //触发工位二相机及光源
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                DIO_Thread.Do[2] = 1;
            }
            else
            {
                DIO_Thread.Do[2] = 0;
            }
        }
        //触发工位三相机及光源
        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                DIO_Thread.Do[3] = 1;
            }
            else
            {
                DIO_Thread.Do[3] = 0;
            }
        }
        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                DIO_Thread.Do[4] = 1;
            }
            else
            {
                DIO_Thread.Do[4] = 0;
            }
        }
        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox5.Checked)
            {
                DIO_Thread.Do[5] = 1;
            }
            else
            {
                DIO_Thread.Do[5] = 0;
            }
        }
        //绿(三色灯)
        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox6.Checked)
            {
                DIO_Thread.Do[6] = 1;
            }
            else
            {
                DIO_Thread.Do[6] = 0;
            }
        }
        //绿(三色灯)
        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox7.Checked)
            {
                DIO_Thread.Do[7] = 1;
            }
            else
            {
                DIO_Thread.Do[7] = 0;
            }
        }
        //黄(三色灯)
        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox8.Checked)
            {
                DIO_Thread.Do[8] = 1;
            }
            else
            {
                DIO_Thread.Do[8] = 0;
            }
        }
        //嗡鸣器(三色灯)预备
        private void checkBox9_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox9.Checked)
            {
                DIO_Thread.Do[9] = 1;
            }
            else
            {
                DIO_Thread.Do[9] = 0;
            }
        }
        //NG踢料吹气讯号
        private void checkBox10_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox10.Checked)
            {
                DIO_Thread.Do[10] = 1;
            }
            else
            {
                DIO_Thread.Do[10] = 0;
            }
        }
        private void checkBox11_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox11.Checked)
            {
                DIO_Thread.Do[11] = 1;
            }
            else
            {
                DIO_Thread.Do[11] = 0;
            }
        }
        private void checkBox12_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox12.Checked)
            {
                DIO_Thread.Do[12] = 1;
            }
            else
            {
                DIO_Thread.Do[12] = 0;
            }
        }
        private void checkBox13_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox13.Checked)
            {
                DIO_Thread.Do[13] = 1;
            }
            else
            {
                DIO_Thread.Do[13] = 0;
            }
        }
        //开/关真空帮浦
        private void checkBox14_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox14.Checked)
            {
                DIO_Thread.Do[14] = 1;
            }
            else
            {
                DIO_Thread.Do[14] = 0;
            }
        }
        private void checkBox15_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox15.Checked)
            {
                DIO_Thread.Do[15] = 1;
            }
            else
            {
                DIO_Thread.Do[15] = 0;
            }
        }
        #endregion

        private void allDOBtn_Click(object sender, EventArgs e)
        {
            for (ushort i = 0; i < 16; i++)
            {
                DIO_Thread.Do[i] = 1;
                foreach (Control ctemp in AllDOGb.Controls)
                {
                    if (ctemp.Name == "checkBox" + i.ToString())
                    {
                        ((CheckBox)ctemp).Checked = true;
                    }
                }
            }
        }

        private void cancleAllDOBtn_Click(object sender, EventArgs e)
        {
            for (ushort i = 0; i < 16; i++)
            {
                DIO_Thread.Do[i] = 0;
                foreach (Control ctemp in AllDOGb.Controls)
                {
                    if (ctemp.Name == "checkBox" + i.ToString())
                    {
                        ((System.Windows.Forms.CheckBox)ctemp).Checked = false;
                    }
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //#region

            //ushort usAxis = 0;  //轴号
            //double dCurrentVel = 0;     //速度
            //uint nCmdPos = 0;         //指令位置
            //uint nEnPos = 0;          //编码器反馈位置

            //dCurrentVel = LTDMC.dmc_read_current_speed(DMC300_Info.usCardId[0], usAxis); // 读取轴当前速度
            //currSpeed.Text = dCurrentVel.ToString();
            //nCmdPos = LTDMC.dmc_get_position(DMC300_Info.usCardId[0], usAxis);           //读取指定轴指令位置值
            //currLocationTb.Text = nCmdPos.ToString();
            //nEnPos = LTDMC.dmc_get_encoder(DMC300_Info.usCardId[0], usAxis);             //读取指定轴的编码器反馈值
            //feedBackTb.Text = nEnPos.ToString();

            //if (LTDMC.dmc_check_done(DMC300_Info.usCardId[0], usAxis) == 0)                      //读取指定轴运动状态
            //{
            //    runStateTb.Text = "运行中";
            //}
            //else
            //{
            //    runStateTb.Text = "停止中";
            //}
            //#endregion


            UpdateAxisState(0, currSpeed, currLocationTb, feedBackTb, runStateTb);
            UpdateAxisState(1, currSpeed_02, currLocationTb_02, feedBackTb_02, runStateTb_02);
            UpdateAxisState(2, currSpeed_03, currLocationTb_03, feedBackTb_03, runStateTb_03);
            UpdateAxisState(3, currSpeed_04, currLocationTb_04, feedBackTb_04, runStateTb_04);


            //////////DI顯示///////////////////////////////////////////////////////
            //DI_0 急停//   //DI_1 启动开关//   //DI_2 手动/自动(切换开关)//   //DI_3 停止开关//   //DI_4 总气压传感器//   //DI_5 总真空传感器//
            //DI_11 下光源输送线入料光纤传感器//   //DI_12 上光源输送线入料光纤传感器//   
            //DI_13 踢料光纤传感器(预备)//   //DI_14 确认踢料结果光纤传感器(预备)//
            for (ushort i = 0; i < 16; i++)
            {
                foreach (Control ctemp in DI_gB.Controls)
                {
                    if (ctemp.Name == "textBox" + (i + 100).ToString())
                    {
                        ((System.Windows.Forms.TextBox)ctemp).Text = DIO_Thread.Di[i].ToString();
                    }
                }
            }
        }



        public static void UpdateAxisState(ushort usAxis,TextBox currSpeedTB, TextBox currLocationTB, TextBox feedBackTB, TextBox runStateTB)
        {
            //ushort usAxis = 0;  //轴号
            double dCurrentVel = 0;     //速度
            UInt32 nCmdPos = 0;         //指令位置
            UInt32 nEnPos = 0;          //编码器反馈位置

            dCurrentVel = LTDMC.dmc_read_current_speed(DMC300_Info.usCardId[0], usAxis); // 读取轴当前速度
            currSpeedTB.Text = dCurrentVel.ToString();
            nCmdPos = LTDMC.dmc_get_position(DMC300_Info.usCardId[0], usAxis);           //读取指定轴指令位置值
            currLocationTB.Text = nCmdPos.ToString();
            nEnPos = LTDMC.dmc_get_encoder(DMC300_Info.usCardId[0], usAxis);             //读取指定轴的编码器反馈值
            feedBackTB.Text = nEnPos.ToString();

            if (LTDMC.dmc_check_done(DMC300_Info.usCardId[0], usAxis) == 0)                      //读取指定轴运动状态
            {
                runStateTB.Text = "运行中";
            }
            else
            {
                runStateTB.Text = "停止中";
            }
        }
        public static void StateClean(ushort usAxis)
        {
            //ushort usAxis = 0;                           //轴号
            int dPos = 0;                                     // 当前位置
            LTDMC.dmc_set_position(DMC300_Info.usCardId[0], usAxis, dPos); //设置指定轴的当前指令位置值歸零
            LTDMC.dmc_set_encoder(DMC300_Info.usCardId[0], usAxis, dPos);  //設置指定軸編碼器歸零
        }
        public static void ChangeSpeed(ushort usAxis,string changeSpeed, string changeTime)
        {
            //ushort usAxis = 0; //轴号
            double dNewVel = Double.Parse(changeSpeed);                // 新的运行速度
            double dTaccDec = Double.Parse(changeTime);              //变速时间
            LTDMC.dmc_change_speed(DMC300_Info.usCardId[0], usAxis, dNewVel, dTaccDec);  //在线变速
        }
        public static void ServoStart(ushort usAxis)
        {
            if (LTDMC.dmc_read_sevon_pin(DMC300_Info.usCardId[0], usAxis) == 1)      //當讀取狀態為1時，當下始能未開啟
            {
                LTDMC.dmc_write_sevon_pin(DMC300_Info.usCardId[0], usAxis, 0);       //下達0時是開啟始能

            }
            else
            {
                LTDMC.dmc_write_sevon_pin(DMC300_Info.usCardId[0], usAxis, 1);       ////下達1時是關閉始能
            }
        }
        public static void ContinuousMotion(ushort usAxis,string startSpeed, string runSpeed, string upTime, string downTime, string stopSpeed, string S_Time,bool isforward)
        {
            //ushort usAxis = 0; //轴号
            double dStartVel;           //起始速度
            double dMaxVel;             //运行速度
            double dTacc;               //加速时间
            double dTdec;               //减速时间
            double dStopVel;            //停止速度
            double dSpara;              //S段时间       
            ushort usDir;               //运动方向

            dStartVel = Double.Parse(startSpeed);
            dMaxVel = Double.Parse(runSpeed);
            dTacc = Double.Parse(upTime);
            dTdec = Double.Parse(downTime);
            dStopVel = Double.Parse(stopSpeed);
            dSpara = Double.Parse(S_Time);

            if (isforward)
            {
                usDir = 1;
            }
            else
            {
                usDir = 0;
            }

            LTDMC.dmc_set_profile(DMC300_Info.usCardId[0], usAxis, dStartVel, dMaxVel, dTacc, dTdec, dStopVel);//设置速度参数
            LTDMC.dmc_set_s_profile(DMC300_Info.usCardId[0], usAxis, 0, dSpara);                                    //设置S段速度参数
            LTDMC.dmc_vmove(DMC300_Info.usCardId[0], usAxis, usDir);                                                  //定速运动
        }
        public static void stopAxis(ushort usAxis,string downTime)
        {
            //ushort usAxis = 0;                             //轴号
            ushort usStopMode = 0;                                 //制动方式，0：减速停止，1：紧急停止
            double dTdec = Double.Parse(downTime);       //减速时间         
            LTDMC.dmc_set_dec_stop_time(DMC300_Info.usCardId[0], usAxis, dTdec);  //设置减速停止时间
            LTDMC.dmc_stop(DMC300_Info.usCardId[0], usAxis, usStopMode);
        }
        public static void circleOnce(ushort usAxis, string startSpeed, string runSpeed, string upTime, string downTime, string stopSpeed, string S_Time, string motorCircle, bool isforward)
        {
            //ushort usAxis = 0; //轴号
            double dStartVel;           //起始速度
            double dMaxVel;             //运行速度
            double dTacc;               //加速时间
            double dTdec;               //减速时间
            double dStopVel;            //停止速度
            double dSpara;              //S段时间       
            ushort usDir;               //运动方向          
            int nDist;               //目标位置
            ushort usPosiMode;          //运动模式0：相对坐标模式，1：绝对坐标模式

            dStartVel = Double.Parse(startSpeed);
            dMaxVel = Double.Parse(runSpeed);
            dTacc = Double.Parse(upTime);
            dTdec = Double.Parse(downTime);
            dStopVel = Double.Parse(stopSpeed);
            dSpara = Double.Parse(S_Time);
            nDist = int.Parse(motorCircle);
            if (isforward)
            {
                usDir = 1;
            }
            else
            {
                usDir = 0;
            }

            usPosiMode = 0;

            LTDMC.dmc_set_profile(DMC300_Info.usCardId[0], usAxis, dStartVel, dMaxVel, dTacc, dTdec, dStopVel);//设置速度参数
            LTDMC.dmc_set_s_profile(DMC300_Info.usCardId[0], usAxis, 0, dSpara);                               //设置S段速度参数
            LTDMC.dmc_pmove(DMC300_Info.usCardId[0], usAxis, nDist, usPosiMode);                               //定长运动
        }

        #region Axis1  下光源输送线
        private void StateClearBtn_Click(object sender, EventArgs e)
        {
            #region
            //ushort usAxis = 0;                           //轴号
            //int dPos = 0;                                     // 当前位置
            //LTDMC.dmc_set_position(DMC300_Info.usCardId[0], usAxis, dPos); //设置指定轴的当前指令位置值歸零
            //LTDMC.dmc_set_encoder(DMC300_Info.usCardId[0], usAxis, dPos);  //設置指定軸編碼器歸零
            #endregion

            StateClean(0);
        }

        private void ChangeSpeedBtn_Click(object sender, EventArgs e)
        {
            #region
            //ushort usAxis = 0; //轴号
            //double dNewVel = Double.Parse(changeSpeedTb.Text);                // 新的运行速度
            //double dTaccDec = Double.Parse(changeTimeTb.Text);              //变速时间
            //LTDMC.dmc_change_speed(DMC300_Info.usCardId[0], usAxis, dNewVel, dTaccDec);  //在线变速
            #endregion

            ChangeSpeed(0, changeSpeedTb.Text, changeTimeTb.Text);
        }

        private void ServoStartBtn_Click(object sender, EventArgs e)
        {
            #region
            //if (LTDMC.dmc_read_sevon_pin(DMC300_Info.usCardId[0], 0) == 1)      //當讀取狀態為1時，當下始能未開啟
            //{
            //    LTDMC.dmc_write_sevon_pin(DMC300_Info.usCardId[0], 0, 0);       //下達0時是開啟始能
            //}
            //else
            //{
            //    LTDMC.dmc_write_sevon_pin(DMC300_Info.usCardId[0], 0, 1);       ////下達1時是關閉始能
            //}
            #endregion

            ServoStart(0);
        }

        private void ContinuousMotionBtn_Click(object sender, EventArgs e)
        {
            ContinuousMotion(0, startSpeedTb.Text, runSpeedTb.Text, upTimeTb.Text, downTimeTb.Text, stopSpeedTb.Text, S_TimeTb.Text, forwardRb.Checked);
            #region
            //ushort usAxis = 0; //轴号
            //double dStartVel;           //起始速度
            //double dMaxVel;             //运行速度
            //double dTacc;               //加速时间
            //double dTdec;               //减速时间
            //double dStopVel;            //停止速度
            //double dSpara;              //S段时间       
            //ushort usDir;               //运动方向

            //dStartVel = Double.Parse(startSpeedTb.Text);
            //dMaxVel = Double.Parse(runSpeedTb.Text);
            //dTacc = Double.Parse(upTimeTb.Text);
            //dTdec = Double.Parse(downTimeTb.Text);
            //dStopVel = Double.Parse(stopSpeedTb.Text);
            //dSpara = Double.Parse(S_TimeTb.Text);

            //if (forwardRb.Checked)
            //{
            //    usDir = 1;
            //}
            //else
            //{
            //    usDir = 0;
            //}

            //LTDMC.dmc_set_profile(DMC300_Info.usCardId[0], usAxis, dStartVel, dMaxVel, dTacc, dTdec, dStopVel);//设置速度参数
            //LTDMC.dmc_set_s_profile(DMC300_Info.usCardId[0], usAxis, 0, dSpara);                                    //设置S段速度参数
            //LTDMC.dmc_vmove(DMC300_Info.usCardId[0], usAxis, usDir);                                                  //定速运动
            #endregion

        }

        private void stopAxisBtn_Click(object sender, EventArgs e)
        {
            stopAxis(0, downTimeTb.Text);
            #region
            //ushort usAxis = 0;                             //轴号
            //ushort usStopMode = 0;                                 //制动方式，0：减速停止，1：紧急停止
            //double dTdec = Double.Parse(downTimeTb.Text);       //减速时间         
            //LTDMC.dmc_set_dec_stop_time(DMC300_Info.usCardId[0], usAxis, dTdec);  //设置减速停止时间
            //LTDMC.dmc_stop(DMC300_Info.usCardId[0], usAxis, usStopMode);
            #endregion

        }

        private void circleOnceBtn_Click(object sender, EventArgs e)
        {
            circleOnce(0, startSpeedTb.Text, runSpeedTb.Text, upTimeTb.Text, downTimeTb.Text, stopSpeedTb.Text, S_TimeTb.Text, motorCircleTb.Text, forwardRb.Checked);
            #region 
            //ushort usAxis = 0; //轴号
            //double dStartVel;           //起始速度
            //double dMaxVel;             //运行速度
            //double dTacc;               //加速时间
            //double dTdec;               //减速时间
            //double dStopVel;            //停止速度
            //double dSpara;              //S段时间       
            //ushort usDir;               //运动方向            
            //int nDist;               //目标位置
            //ushort usPosiMode;          //运动模式0：相对坐标模式，1：绝对坐标模式

            //dStartVel = Double.Parse(startSpeedTb.Text);
            //dMaxVel = Double.Parse(runSpeedTb.Text);
            //dTacc = Double.Parse(upTimeTb.Text);
            //dTdec = Double.Parse(downTimeTb.Text);
            //dStopVel = Double.Parse(stopSpeedTb.Text);
            //dSpara = Double.Parse(S_TimeTb.Text);
            //nDist = int.Parse(motorCircleTb.Text);
            //if (forwardRb.Checked)
            //{
            //    usDir = 1;
            //}
            //else
            //{
            //    usDir = 0;
            //}

            //usPosiMode = 0;

            //LTDMC.dmc_set_profile(DMC300_Info.usCardId[0], usAxis, dStartVel, dMaxVel, dTacc, dTdec, dStopVel);//设置速度参数
            //LTDMC.dmc_set_s_profile(DMC300_Info.usCardId[0], usAxis, 0, dSpara);                               //设置S段速度参数
            //LTDMC.dmc_pmove(DMC300_Info.usCardId[0], usAxis, nDist, usPosiMode);                               //定长运动
            #endregion
        }
        #endregion

        #region Axis2  上光源输送线
        private void StateClearBtn_02_Click(object sender, EventArgs e)
        {
            StateClean(1);
        }

        private void ChangeSpeedBtn_02_Click(object sender, EventArgs e)
        {
            ChangeSpeed(1, changeSpeedTb_02.Text, changeTimeTb_02.Text);
        }

        private void ServoStartBtn_02_Click(object sender, EventArgs e)
        {
            ServoStart(1);
        }

        private void ContinuousMotionBtn_02_Click(object sender, EventArgs e)
        {
            ContinuousMotion(1, startSpeedTb_02.Text, runSpeedTb_02.Text, upTimeTb_02.Text, downTimeTb_02.Text, stopSpeedTb_02.Text, S_TimeTb_02.Text, forwardRb_02.Checked);

        }

        private void stopAxisBtn_02_Click(object sender, EventArgs e)
        {
            stopAxis(1, downTimeTb_02.Text);
        }

        private void circleOnceBtn_02_Click(object sender, EventArgs e)
        {
            circleOnce(1, startSpeedTb_02.Text, runSpeedTb_02.Text, upTimeTb_02.Text, downTimeTb_02.Text, stopSpeedTb_02.Text, S_TimeTb_02.Text, motorCircleTb_02.Text, forwardRb_02.Checked);

        }
        #endregion

        #region  Axis3  入料输送线(备)
        private void StateClearBtn_03_Click(object sender, EventArgs e)
        {
            StateClean(2);
        }

        private void ChangeSpeedBtn_03_Click(object sender, EventArgs e)
        {
            ChangeSpeed(2, changeSpeedTb_03.Text, changeTimeTb_03.Text);
        }

        private void ServoStartBtn_03_Click(object sender, EventArgs e)
        {
            ServoStart(2);
        }

        private void ContinuousMotionBtn_03_Click(object sender, EventArgs e)
        {
            ContinuousMotion(2, startSpeedTb_03.Text, runSpeedTb_03.Text, upTimeTb_03.Text, downTimeTb_03.Text, stopSpeedTb_03.Text, S_TimeTb_03.Text, forwardRb_03.Checked);

        }

        private void stopAxisBtn_03_Click(object sender, EventArgs e)
        {
            stopAxis(2, downTimeTb_03.Text);
        }

        private void circleOnceBtn_03_Click(object sender, EventArgs e)
        {
            circleOnce(2, startSpeedTb_03.Text, runSpeedTb_03.Text, upTimeTb_03.Text, downTimeTb_03.Text, stopSpeedTb_03.Text, S_TimeTb_03.Text, motorCircleTb_03.Text, forwardRb_03.Checked);

        }
        #endregion

        #region  Axis4  预备
        private void StateClearBtn_04_Click(object sender, EventArgs e)
        {
            StateClean(3);
        }

        private void ChangeSpeedBtn_04_Click(object sender, EventArgs e)
        {
            ChangeSpeed(3, changeSpeedTb_04.Text, changeTimeTb_04.Text);
        }

        private void ServoStartBtn_04_Click(object sender, EventArgs e)
        {
            ServoStart(3);
        }

        private void ContinuousMotionBtn_04_Click(object sender, EventArgs e)
        {
            ContinuousMotion(3, startSpeedTb_04.Text, runSpeedTb_04.Text, upTimeTb_04.Text, downTimeTb_04.Text, stopSpeedTb_04.Text, S_TimeTb_04.Text, forwardRb_04.Checked);

        }

        private void stopAxisBtn_04_Click(object sender, EventArgs e)
        {
            stopAxis(3, downTimeTb_04.Text);
        }

        private void circleOnceBtn_04_Click(object sender, EventArgs e)
        {
            circleOnce(3, startSpeedTb_04.Text, runSpeedTb_04.Text, upTimeTb_04.Text, downTimeTb_04.Text, stopSpeedTb_04.Text, S_TimeTb_04.Text, motorCircleTb_04.Text, forwardRb_04.Checked);

        }

        private void storageBtn_Click(object sender, EventArgs e)
        {
            SaveParameters();
        }



        #endregion

        //触发取像参数储存
    }
    public class AxisParameters
    {
        public List<AxisConfig> Axes { get; set; } = new List<AxisConfig>();
        public List<CameraConfig> Cameras { get; set; } = new List<CameraConfig>();
    }

    public class AxisConfig
    {
        public string StartSpeed { get; set; }
        public string RunSpeed { get; set; }
        public string UpTime { get; set; }
        public string DownTime { get; set; }
        public string StopSpeed { get; set; }
        public string S_Time { get; set; }
        public string MotorCircle { get; set; }
        public bool IsForward { get; set; }
        public string ChangeSpeed { get; set; }
        public string ChangeTime { get; set; }
    }

    public class CameraConfig
    {
        public string C { get; set; }
    }
}
